import cv2
import sys
import time

dt = "2019-01-23 15:29:00"
# 转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
# 转换成时间戳
timestamp = time.mktime(timeArray)
# print(timeArray)
# print(timestamp)
user, pwd, ip, channel = "admin", "iotcam213", "10.15.199.182", 1

cap_path = "rtsp://%s:%s@%s//Streaming/Channels/%d" % (user, pwd, ip, channel)  # HIKIVISION new version 2017
cap_1 = cv2.VideoCapture(cap_path)
cap_1.set(3, 1920)
cap_1.set(4, 1080)

write_ok = True

sz = (int(cap_1.get(cv2.CAP_PROP_FRAME_WIDTH)),
      int(cap_1.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 15
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
# fourcc = cv2.VideoWriter_fourcc(*'mpeg')

vout_1 = cv2.VideoWriter()
vout_1.open('./output_mpeg.mp4', fourcc, fps, sz, True)


cnt = 0
while (True):
    if (write_ok):
        print("video")
        # 获取当前时间
        time_now = int(time.time())
        # 转换成localtime
        # time_local = time.localtime(time_now)
        # print(time_now)
        if time_now >= timestamp:
            while (cnt < 900):
                cnt += 1
                print(cnt)

                ret_1, frame_1 = cap_1.read()
                vout_1.write(frame_1)

            vout_1.release()

            sys.exit()
    else:

        ret_1, frame_1 = cap_1.read()
        cv2.imshow("cam_1", frame_1)

    if cv2.waitKey(1) & 0xFF == ord("w"):
        write_ok = write_ok is not True
